package com.zjw.forum.controller;

import com.sun.istack.internal.NotNull;
import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.model.Board;
import com.zjw.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
@Api("板块接口")
public class BoardController {
    //如果没有配置，默认值为9
    @Value("${forum.board.rows:9}")
    private Integer rows;
    @Autowired
    IBoardService boardService;
    @ApiOperation("获取首页板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> getTopList(){
        List<Board> boardName = boardService.getBoardName(rows);
        if(boardName==null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        return AppResult.success(boardName);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getById")
    public AppResult getById(@ApiParam("板块id") @NotNull @RequestParam("id") Long id){
        Board board = boardService.getBoardInfoById(id);
        if(board==null||board.getState()==1||board.getDeleteState()==1){
            log.error(ResultCode.FAILED_BOARD_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        return AppResult.success(board);
    }

}
